package com.bite.system.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bite.common.core.constants.Constants;
import com.bite.common.core.enums.ResultCode;
import com.bite.common.security.exception.ServiceException;
import com.bite.system.domain.question.Question;
import com.bite.system.domain.question.dto.QuestionAddDTO;
import com.bite.system.domain.question.dto.QuestionEditDTO;
import com.bite.system.domain.question.dto.QuestionQueryDTO;
import com.bite.system.domain.question.es.QuestionES;
import com.bite.system.domain.question.vo.QuestionDetailVO;
import com.bite.system.domain.question.vo.QuestionVO;
import com.bite.system.elasticsearch.QuestionRepository;
import com.bite.system.manager.QuestionCacheManager;
import com.bite.system.mapper.question.QuestionMapper;
import com.bite.system.service.question.IQuestionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private QuestionCacheManager questionCacheManager;
    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        String excludeIdStr= questionQueryDTO.getExcludeIdStr();
        if(StrUtil.isNotEmpty(excludeIdStr)){
            //String类型数组应该转换成Long形状，而且还不一定重复
            String[]excludeIdArr=excludeIdStr.split(Constants.SPLIT_SEM);  //分号应该是一个常量
            Set<Long>excludeIdSet= Arrays.stream(excludeIdArr)
                    //map把String 转换成Long类型
                    .map(Long::valueOf)
                    //转换成Set
                    .collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }

        PageHelper.startPage(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize());
        //question表太多的可变，导致他的SQL会有些复杂，而且维护麻烦
        List<QuestionVO>questionVOList =questionMapper.selectQuestionList(questionQueryDTO);
        return questionVOList;
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        List<Question> questionList=questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle,questionAddDTO.getTitle()));
        if(CollectionUtils.isNotEmpty(questionList)){
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question=new Question();
        BeanUtil.copyProperties(questionAddDTO,question);
        int insert=questionMapper.insert(question);
        if(insert<=0){
            return false;
        }
        QuestionES questionES=new QuestionES();
        BeanUtil.copyProperties(question,questionES);
        //插入进ES里面，我认为ES相当于是一个小数据库
        questionRepository.save(questionES);
        questionCacheManager.addCache(question.getQuestionId());
        return true;
    }

    @Override
    public  QuestionDetailVO detail(Long questionId) {
        Question question=search(questionId);
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();
        BeanUtil.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question oldquestion=search(questionEditDTO.getQuestionId());
        oldquestion.setTitle(questionEditDTO.getTitle());
        oldquestion.setDifficulty(questionEditDTO.getDifficulty());
        oldquestion.setTimeLimit(questionEditDTO.getTimeLimit());
        oldquestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        oldquestion.setContent(questionEditDTO.getContent());
        oldquestion.setQuestionCase(questionEditDTO.getQuestionCase());
        oldquestion.setDefaultCode(questionEditDTO.getDefaultCode());
        oldquestion.setMainFuc(questionEditDTO.getMainFuc());
        QuestionES questionES=new QuestionES();
        BeanUtil.copyProperties(oldquestion,questionES);
        questionRepository.save(questionES);
        return questionMapper.updateById(oldquestion);
    }

    @Override
    public synchronized int delete(Long questionId) {
       search(questionId);
       questionRepository.deleteById(questionId);
       questionCacheManager.deleteCache(questionId);
       return questionMapper.deleteById(questionId);
    }



    public Question search(Long questionId){
        Question question=questionMapper.selectById(questionId);
        if(question==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        return question;
    }

}
